<?php

/*
 * 图片 - 数据迁移
 * @author birdy <freebirdy@foxmail.com>
 * @link http://www.aiyuangong.com
 */

namespace app\admin\command\transfer;

use think\console\Input;
use think\console\Output;
use think\Cache;

class Images extends Table
{
    private $rootPath = '';
    private $saveRoot = ROOT_PATH . 'public/uploads/';

    /**
     * 处理任务计划中的待执行任务
     * 
     * @return void
     */
    public function execute(Input $input, Output $output)
    {
        $images = [];
        if (is_file($this->imagesFile)) {
            $images = include $this->imagesFile;
        }

        if (!$images) {
            return $this->_showLog('no images need to transfer', 'step');
        }

        $this->_showLog('images transfer start', 'step');
        $this->rootPath = config('old_data_path');
        if (!is_dir($this->rootPath)) {
            return $this->_showLog('dir not exist: ' . $this->rootPath, 'error');
        }

        foreach ($images as $table => $images) {
            foreach ($images as $method => $image) {
                $method = '_move_image_' . $method;
                if (method_exists($this, $method)) {
                    call_user_func([$this, $method], $image);
                } else {
                    $this->_showLog("method '" . $method . "' does not exist", "error");
                }
            }
        }

        is_file($this->imagesFile) && @unlink($this->imagesFile);
        $this->_showLog('images transfer end', 'step');
    }

    //移动商品图片
    private function _move_image_goods($images)
    {
        $imgs_path = $this->rootPath . 'data/upload/shop/store/goods/';
        foreach ($images as $images) {
            $img = explode('_', $images, 2);
            $file = $imgs_path . $img[0] . '/' . $images;
            if (is_file($file)) {
                $save_path = 'album/' . $img[0] . '/' . $images;
                $target = $this->saveRoot . $save_path;
                if (!is_file($target)) {
                    $dir = dirname($target);
                    is_dir($dir) || @mkdir($dir, 755, true);
                    @copy($file, $target);
                    $this->_showLog("copy '" . $file . "' to " . $target, "step");
                }
            } else {
                $this->_showLog("file '" . $file . "' does not exist", "error");
            }
        }
    }

    //移动店铺logo
    private function _move_image_store_label($images)
    {
        $imgs_path = $this->rootPath . 'data/upload/shop/store';
        foreach ($images as $images) {
            $images = basename($images);
            $file = $imgs_path . '/' . $images;
            if (is_file($file)) {
                $save_path = 'store/label/' . $images;
                $target = $this->saveRoot . $save_path;
                if (!is_file($target)) {
                    $dir = dirname($target);
                    is_dir($dir) || @mkdir($dir, 755, true);
                    @copy($file, $target);
                    $this->_showLog("copy '" . $file . "' to " . $target, "step");
                }
            } else {
                $this->_showLog("file '" . $file . "' does not exist", "error");
            }
        }
    }

    //移动店铺logo
    private function _move_image_store_slide($images)
    {
        $imgs_path = $this->rootPath . 'data/upload/shop/store/slide';
        foreach ($images as $images) {
            foreach ($images as $image) {
                $image = basename($image);
                $file = $imgs_path . '/' . $image;
                if (is_file($file)) {
                    $save_path = 'store/slide/' . $image;
                    $target = $this->saveRoot . $save_path;
                    if (!is_file($target)) {
                        $dir = dirname($target);
                        is_dir($dir) || @mkdir($dir, 755, true);
                        @copy($file, $target);
                        $this->_showLog("copy '" . $file . "' to " . $target, "step");
                    }
                } else {
                    $this->_showLog("file '" . $file . "' does not exist", "error");
                }
            }
        }
    }

    //移动品牌图片
    private function _move_image_brand($images)
    {
        $imgs_path = $this->rootPath . 'data/upload/shop/brand';
        foreach ($images as $images) {
            $file = $imgs_path . '/' . $images;
            if (is_file($file)) {
                $save_path = 'brand/' . $images;
                $target = $this->saveRoot . $save_path;
                if (!is_file($target)) {
                    $dir = dirname($target);
                    is_dir($dir) || @mkdir($dir, 755, true);
                    @copy($file, $target);
                    $this->_showLog("copy '" . $file . "' to " . $target, "step");
                }
            } else {
                $this->_showLog("file '" . $file . "' does not exist", "error");
            }
        }
    }

    //会员头像
    private function _move_image_member($images)
    {
        //dump($images);
    }

    //线下支付凭证
    private function _move_image_order_pay($images)
    {
        $imgs_path = $this->rootPath . 'data/upload/shop/crimg';
        foreach ($images as $images) {
            $images = basename($images);
            $file = $imgs_path . '/' . $images;
            if (is_file($file)) {
                $save_path = 'payment/old/' . $images;
                $target = $this->saveRoot . $save_path;
                if (!is_file($target)) {
                    $dir = dirname($target);
                    is_dir($dir) || @mkdir($dir, 755, true);
                    @copy($file, $target);
                    $this->_showLog("copy '" . $file . "' to " . $target, "step");
                }
            } else {
                $this->_showLog("file '" . $file . "' does not exist", "error");
            }
        }
    }

    //店铺图片
    private function _move_image_store($images)
    {
        //dump($images);
    }

    //店铺申请加入资料图片
    private function _move_image_joinin($images)
    {
        $imgs_path = $this->rootPath . 'data/upload/shop/store_joinin';
        foreach ($images as $images) {
            $file = $imgs_path . '/' . $images;
            if (is_file($file)) {
                $save_path = 'joinin/old/' . $images;
                $target = $this->saveRoot . $save_path;
                if (!is_file($target)) {
                    $dir = dirname($target);
                    is_dir($dir) || @mkdir($dir, 755, true);
                    @copy($file, $target);
                    $this->_showLog("copy '" . $file . "' to " . $target, "step");
                }
            } else {
                $this->_showLog("file '" . $file . "' does not exist", "error");
            }
        }
    }

    //快递运单模板图片
    private function _move_image_waybill($images)
    {
        $imgs_path = $this->rootPath . 'data/upload/shop/waybill';
        foreach ($images as $images) {
            $file = $imgs_path . '/' . $images;
            if (is_file($file)) {
                $save_path = 'waybill/old/' . $images;
                $target = $this->saveRoot . $save_path;
                if (!is_file($target)) {
                    $dir = dirname($target);
                    is_dir($dir) || @mkdir($dir, 755, true);
                    @copy($file, $target);
                    $this->_showLog("copy '" . $file . "' to " . $target, "step");
                }
            } else {
                $this->_showLog("file '" . $file . "' does not exist", "error");
            }
        }
    }

    //移动编辑器内容里面的图片
    private function _move_image_editor_content($images)
    {
        $imgs = [];
        $thumb = [];
        foreach ($images as $images) {
            $rs = [];
            if (preg_match('/^(\d+)_(\d+)_(\d+)(.*)/is', $images, $rs)) {
                $pic = $rs[1] . "_" . $rs[2] . $rs[4];
                $path = 'album/' . $rs[1] . '/';
                $imgs[] = $pic;
                $thumb[] = [$path . $pic, $rs[3]];
            }
        }

        $this->_move_image_goods($imgs);
        foreach ($thumb as $thumb) {
            img_url($thumb[0], $thumb[1]);
            //$this->_showLog('create thumb: '.$thumb[0], 'step');
        }
    }

    //命令行配置说明
    protected function configure()
    {
        $this->setName('transfer:images')
                ->setDescription('Transfer the images of old shop');
    }

}
